/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

import edu.idp.client.event.ServerStatusEvent;
import edu.idp.client.interfaces.ServerStatusListener;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 *
 * @author Kristopher T Babic
 */
public class MainMenuFrame extends Frame implements ServerStatusListener {

    private ToolPanel leftPanel;
    private InterDrawPanel middlePanel;
    private ColorPanel bottomPanel;

    private Image buffer;
    private Graphics bufferGraphics;
    private Dimension size;
    private Applet ap;
    //private Frame myFrame;
    private MenuItem sendItem, receiveItem, animAdd, selectImage, checkStatus;    //newImage, openImage,


    public MainMenuFrame() {
        this("", null);
    }

    public MainMenuFrame(String s, Applet ap) {
        super(s);

        this.ap = ap;

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                try {
                    middlePanel.stop();
                }
                catch (Exception e) {
                }
                dispose();
                //System.exit(0);
            }
        });

    }

    public void init() {
        //Util.setCursor(Cursor.WAIT_CURSOR, this);

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

/*	START MENU TESTING 		*/
        //myFrame = new MenuFrame("Option Menu");
        MenuBar mbar = new MenuBar();
        Menu netMenu = new Menu("Network", true);
        Menu fileMenu = new Menu("Image", true);

        fileMenu.add(selectImage = new MenuItem("Select", new MenuShortcut(KeyEvent.VK_S)));
/*
	  fileMenu.add(newImage = new MenuItem ("New", new MenuShortcut(KeyEvent.VK_N)));
    fileMenu.add(openImage = new MenuItem ("Open", new MenuShortcut(KeyEvent.VK_O)));
 */
//		fileMenu.add(printItem = new MenuItem("Print"));

// 4-9-2000		netMenu.add(sendItem = new MenuItem("Send"));
        netMenu.add(receiveItem = new MenuItem("Update"));
        netMenu.add(checkStatus = new MenuItem("Check"));

        Menu animMenu = new Menu("Animation", true);

        animMenu.add(animAdd = new MenuItem("Insert"));

        mbar.add(fileMenu);
        mbar.add(netMenu);
        mbar.add(animMenu);
        this.setMenuBar(mbar);

/* END MENU TESTING        */

        setLayout(gbl);
//    setLayout(null);

        middlePanel = new InterDrawPanel(ap);
        leftPanel = new ToolPanel(middlePanel, ap);
        bottomPanel = new ColorPanel(middlePanel, ap);

//		newImage.addActionListener(middlePanel);
//    openImage.addActionListener(middlePanel);
        selectImage.addActionListener(middlePanel);
        checkStatus.addActionListener(middlePanel);
// 4-9-2000		sendItem.addActionListener(middlePanel);
        receiveItem.addActionListener(middlePanel);
        animMenu.addActionListener(middlePanel);


        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 3;
        gbl.setConstraints(leftPanel, gbc);

        leftPanel.setBackground(Color.lightGray);
//    leftPanel.setBounds(0, 0, leftPanel.getMinimumSize().width, leftPanel.getMinimumSize().height);
        add(leftPanel);


        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        gbc.gridheight = 3;
        gbl.setConstraints(middlePanel, gbc);

        middlePanel.setBackground(new Color(255, 255, 255));
//    middlePanel.setBounds(leftPanel.getMinimumSize().width, 0, middlePanel.getMinimumSize().width, middlePanel.getMinimumSize().height);
        add(middlePanel);


        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbl.setConstraints(bottomPanel, gbc);

        bottomPanel.setBackground(Color.lightGray);
//    bottomPanel.setBounds(0, leftPanel.getMinimumSize().height, bottomPanel.getMinimumSize().width, bottomPanel.getMinimumSize().height);
        add(bottomPanel);

        ServerStatusPanel ssp = new ServerStatusPanel(this.ap);
//    System.out.println("ssp created\n");
        middlePanel.addServerStatusListener(ssp);
        bottomPanel.add(ssp);
        ssp.setBounds(450, 0, 100, 50);
    }

    public void start(Integer port, String host) {
        try {
            if (port != null)
                middlePanel.setPort(port.intValue());
        }
        catch (Exception e) {
        }

        if (host != null) {
            try {
                middlePanel.setHost(host);
            }
            catch (Exception e) {
            }
        }
        middlePanel.addServerStatusListener(this);
        middlePanel.init();
        //cmiddlePanel.start();
    }

    public void handleServerStatusEvent(ServerStatusEvent sse) {
//    System.out.println(sse);
        if (sse.getStatus() == ServerStatusEvent.DISCONNECTED || sse.getStatus() == ServerStatusEvent.SERVER_LOST) {
// 4-9-2000      this.sendItem.setEnabled(false);
            this.receiveItem.setEnabled(false);
        }
        else {
// 4-9-2000      this.sendItem.setEnabled(true);
            this.receiveItem.setEnabled(true);
        }
    }


}

/*
class ToolButtonListener implements MouseListener {
    private String            toolAction;
    private AdvancedDrawPanel drawingPanel;
    private Component         myComp;
    private ToolOptionPanel   toolOptions;

    public ToolButtonListener(String Action, AdvancedDrawPanel drawingPanel, ToolOptionPanel toolOpts) {
        toolAction = Action;
        this.drawingPanel = drawingPanel;
        this.toolOptions = toolOpts;
    }

    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}
  public void mouseClicked (MouseEvent event) {
//		mouseClicked(event);
    }
    public void mouseReleased (MouseEvent event) {}

    public void mousePressed (MouseEvent event) {

        ToolButton tool = (ToolButton) event.getSource();
        tool.setDown();


        if (toolAction.equals("E")){
            drawingPanel.drawEllipses();
            toolOptions.setOption(ToolOptionPanel.POLY);
        }
        else if (toolAction.equals("L")){
            drawingPanel.drawLines();
            toolOptions.setOption(ToolOptionPanel.LINE);
        }
        else if (toolAction.equals("R")){
            drawingPanel.drawRectangles();
            toolOptions.setOption(ToolOptionPanel.POLY);
        }
        else if (toolAction.equals("F")){
            InterDrawPanel idp = (InterDrawPanel)drawingPanel;
            idp.drawPencil();
            toolOptions.setOption(ToolOptionPanel.NONE);
        }
    else if (toolAction.equals("ER")) {
      InterDrawPanel idp = (InterDrawPanel)drawingPanel;
      idp.eraseObjects();
      toolOptions.setOption(ToolOptionPanel.NONE);
    }
    }
}
*/


